package com.atguigu.ucenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.dto.UserLoginMemberDTO;
import com.atguigu.ucenter.entity.dto.UserRegisterMemberDTO;
import com.atguigu.ucenter.mapper.MemberMapper;
import com.atguigu.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-07-03
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    MemberMapper memberMapper;
    private static final String MOBILE_PATTERN = "^1[3-9]\\d{9}$";
    @Override
    public String login(UserLoginMemberDTO userLoginMemberDTO) {
        //获取手机号和密码
        String password = userLoginMemberDTO.getPassword();
        String mobile = userLoginMemberDTO.getMobile();
        //手机号密码是否为空
        if(password.isEmpty() || mobile.isEmpty()){
            throw  new GuliException(20001,"手机号或密码不能为空");
        }

        //手机号输入格式是否正确
        if(!isMobileNumberValid(mobile)){
            throw  new GuliException(20001,"手机号输入格式错误");
        }
        //手机号是否是否存在
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Member member = baseMapper.selectOne(wrapper);
        if(member == null){
            throw  new GuliException(20001,"手机号不存在");
        }
        //密码是否正确
        //MD5一种明文加密为密文的工具：401b5026b604aff95f9f7d0c3e2d7d55
        if(!member.getPassword().equals(MD5.encrypt(password))){
            throw  new GuliException(20001,"密码错误");
        }
        //此用户是否为被禁用
        if(member.getIsDisabled()){
            throw  new GuliException(20001,"此用户已警用");
        }

        return JwtUtils.getJwtToken(member.getId(),member.getNickname());
    }
    //添加注册信息
    @Override
    public void register(UserRegisterMemberDTO userRegisterMemberDTO) {
        //获取userRegisterMemberDTO值
        String mobile = userRegisterMemberDTO.getMobile();//手机号
        String password = userRegisterMemberDTO.getPassword();//密码
        String nickname = userRegisterMemberDTO.getNickname();//昵称
        String avatar = userRegisterMemberDTO.getAvatar();//头像
        Integer sex = userRegisterMemberDTO.getSex();//性别
        Integer age = userRegisterMemberDTO.getAge();//年龄
        String sign = userRegisterMemberDTO.getSign();//用户签名
        String code = userRegisterMemberDTO.getCode();//验证码

        //校验是否为空值
        if(password.isEmpty() || mobile.isEmpty() ||nickname.isEmpty() || code.isEmpty()){
            throw  new GuliException(20001,"手机号或密码或昵称或验证码不能为空");
        }
        //手机号格式校验
        if(!isMobileNumberValid(mobile)){
            throw new GuliException(20001,"手机号码格式不通过");
        }
        //密码位数校验
        if(password.length() < 6){
            throw new GuliException(20001,"密码长度不能小于六位");
        }
        //昵称长度校验
        if(nickname.length() >12){
            throw new GuliException(20001,"昵称长度不能小于12个字符");
        }
        //年龄校验
        if (age != null){
            if(age < 12 || age > 60){
                throw new GuliException(20001,"年龄不在办理条件范围内，需要代理人进行办理");
            }
        }
        //用户签名校验
        if (sign != null){
            if(sign.length() < 120){
                throw new GuliException(20001,"用户签名字数不应该超过120个字符");
            }
        }
        //判断用户名是否重复
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("nickname",nickname);
        Integer count = baseMapper.selectCount(memberQueryWrapper);
        if(count > 0){
            throw new GuliException(20001,"用户名重复");
        }
        //判断手机号是否重复
        QueryWrapper<Member> memberQueryWrapper1 = new QueryWrapper<>();
        memberQueryWrapper1.eq("mobile",mobile);
         count = baseMapper.selectCount(memberQueryWrapper1);
        if(count > 0){
            throw new GuliException(20001,"手机号重复");
        }

        //判断验证码是否通过
        String redisMobileCode = redisTemplate.opsForValue().get(mobile);
        if(redisMobileCode == null){
            throw new GuliException(20001,"验证码已过期，请重新发送验证码");
        }
        if(!redisMobileCode.equals(code)){
            throw new GuliException(20001,"验证码输入错误，请重新输入");
        }

        Member member = new Member();
        userRegisterMemberDTO.setPassword(MD5.encrypt(password));
        BeanUtils.copyProperties(userRegisterMemberDTO,member);
//        member.setIsDisabled(false);//用户不禁用
        baseMapper.insert(member);

    }

    @Override
    public Member getOpenIdMember(String openid) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        Member member = baseMapper.selectOne(wrapper);

        return member ;
    }

    @Override
    public Integer countRegister(String day) {

        return memberMapper.countRegisterDay(day);
    }


    //校验手机号格式的专业校验
    private static boolean isMobileNumberValid(String phoneNumber) {
        Pattern compile = Pattern.compile(MOBILE_PATTERN);
        Matcher matcher = compile.matcher(phoneNumber);
        return matcher.matches();
    }




}
